package com.mxpio.erp.plan.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.OrderLine;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.plan.entity.*;
import com.mxpio.erp.common.plan.service.SalePredictionLineService;
import com.mxpio.erp.common.plan.service.SalePredictionLineVOService;
import com.mxpio.erp.common.plan.vo.SalePredictionLineVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "SalePredictionLineController", description = "销售预测行接口")
@RestController
@RequestMapping("/erp/plan/spline")
public class SalePredictionLineController {

	@Autowired
	private SalePredictionLineService salePredictionLineService;
	@Autowired
	private OrderLineService orderLineService;

	@Autowired
	private SalePredictionLineVOService salePredictionLineVOService;
	@GetMapping("list/{bizNo}")
	@Operation(summary = "销售预测行列表", description = "获取销售预测行列表（无分页）", method = "GET")
	public Result<List<SalePredictionLine>> list(@Parameter(description = "查询参数") Criteria criteria,
			@PathVariable(name = "bizNo") String bizNo)
			throws UnsupportedEncodingException {
		criteria.addCriterion("bizNo", Operator.EQ, bizNo);
		List<SalePredictionLine> salePredictionLine = salePredictionLineService.list(SalePredictionLine.class, criteria);
		return Result.OK(salePredictionLine);
	}
	
	@GetMapping("page")
	@Transactional
	@Operation(summary = "销售预测行列表", description = "获取销售预测行列表（分页）", method = "GET")
	public Result<Page<SalePredictionLine>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		Page<SalePredictionLine> salePredictionLine = salePredictionLineService.listPage( page, criteria);

		/*List<SalePredictionLine> contents = salePredictionLine.getContent();
		if(contents.size()>0){
			salePredictionOffsettingFlowService.handleQuantityFromSP(contents);//关联已冲销数量
		}*/
		return Result.OK(salePredictionLine);
	}

	@GetMapping("offsettingpage")
	@Transactional
	@Operation(summary = "销售预测行列表（按预测冲销）", description = "获取销售预测行列表（按预测冲销）（分页）", method = "GET")
	public Result<Page<SalePredictionLineVO>> offsettingPage(@Parameter(description = "查询参数") Criteria criteria,
												 @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("closeStatus",Operator.NE, OrderStatusEnums.CloseStatus.CLOSED.getStatusCode());//过滤掉已关闭数据
		criteria.addCriterion("bpmnStatus",Operator.EQ, OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());//已审核数据
		criteria.addCriterion("bizCloseStatus",Operator.NE, OrderStatusEnums.CloseStatus.CLOSED.getStatusCode());//过滤掉主单已关闭数据
		Page<SalePredictionLineVO> salePredictionLineVOPage = salePredictionLineVOService.listPage( page, criteria);
		return Result.OK(salePredictionLineVOPage);
	}

	@GetMapping("offsettingpickpage")
	@Transactional
	@Operation(summary = "销售预测行列表（按订单冲销选预测明细）", description = "获取销售预测行列表（按订单冲销选预测明细）（分页）", method = "GET")
	public Result<Page<SalePredictionLineVO>> offsettingPickPage(@Parameter(description = "查询参数") Criteria criteria,
															 @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("closeStatus",Operator.NE, OrderStatusEnums.CloseStatus.CLOSED.getStatusCode());//过滤掉已关闭数据
		criteria.addCriterion("bpmnStatus",Operator.EQ, OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());//已审核数据
		criteria.addCriterion("bizCloseStatus",Operator.NE, OrderStatusEnums.CloseStatus.CLOSED.getStatusCode());//过滤掉主单已关闭数据
		criteria.addCriterion("restQuantity",Operator.GT, 0);//过滤掉已冲销完的预测明细
		Page<SalePredictionLineVO> salePredictionLineVOPage = salePredictionLineVOService.listPage( page, criteria);
		return Result.OK(salePredictionLineVOPage);
	}

	@RequestMapping("save")
	@Operation(summary = "保存销售预测行", description = "保存销售预测行")
	public Result<List<SalePredictionLine>> save(@RequestBody List<SalePredictionLine> salePredictionLines) {
		salePredictionLineService.save(salePredictionLines);
		return Result.OK(salePredictionLines);
	}

	@PutMapping("open/{ids}")
	@Operation(summary = "打开销售预测行", description = "打开销售订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "PUT")
	public Result<? extends OrderLine> open(@PathVariable(name = "ids", required = true) String ids) {
		return orderLineService.updateCloseStatus(SalePredictionLine.class, ids, OrderStatusEnums.CloseStatus.OPEN);
	}

	@PutMapping("close/{ids}")
	@Operation(summary = "关闭销售预测行", description = "关闭销售预测行", method = "PUT")
	public Result<? extends OrderLine> close(@Parameter(description="业务单号，多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后") @PathVariable(name = "ids", required = true) String ids) {
		return orderLineService.updateCloseStatus(SalePredictionLine.class, ids, OrderStatusEnums.CloseStatus.CLOSED);
	}
}
